package com.jdyfy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdyfy.entity.Meet;
import com.jdyfy.entity.User;
import com.jdyfy.mapper.AnnouncementMapper;
import com.jdyfy.mapper.MeetMapper;
import com.jdyfy.service.MeetService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MeetServiceImpl extends ServiceImpl<MeetMapper, Meet> implements MeetService {

    @Autowired
    private MeetMapper meetMapper;

    @Autowired
    private com.jdyfy.mapper.ProjectMapper projectMapper;

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Scheduled(cron = "0 0 * * * ?") // "0 0 * * * ?" 每小时检查一次 /// "*/5 * * * * ?" 每五秒检查一次
    public void updateMeetingStatus() {
        log.info("定时任务执行中...");
        // 1. 更新会议室状态
        meetMapper.updateExpiredMeetings();

        // 2. 获取过期会议的所有项目
        List<String> expiredProjectNames = meetMapper.getExpiredProjectNames();
        meetMapper.updateOutMeetings(); //将过期并且已经使用过的会议状态置为2

        // 3. 更新项目表中的annoStatus字段为4
        if (expiredProjectNames != null && !expiredProjectNames.isEmpty()) {
            projectMapper.updateAnnoStatusByProjectNames(expiredProjectNames, 4);
//            for(String expiredProjectName : expiredProjectNames) {
//                announcementMapper.deleteByName(expiredProjectName);
//            }
        }
    }

    // 根据状态查询的会议
    public List<Meet> getMeetByStatus(Integer status) {
        return meetMapper.getMeetByStatus(status);
    }

    // 根据项目名称查询会议
    public Meet getMeetByProjectName(String projectName) {
        return meetMapper.getMeetByProjectName(projectName);
    }

}
